package com.deer.wms.base.system.web.bar;

import com.deer.wms.base.system.model.bar.BarCode;
import com.deer.wms.base.system.model.bar.BarCodeDto;
import com.deer.wms.base.system.model.bar.BarCodeUtil;
import com.deer.wms.base.system.model.DtMap;
import com.deer.wms.base.system.service.bar.BarCodeService;
import com.deer.wms.base.system.service.dtService;
import com.deer.wms.base.system.util.ExcelReadUtil;
import com.deer.wms.base.system.web.bar.BarCodeUtil.DefaultIdGenerator;
import com.deer.wms.base.system.web.bar.BarCodeUtil.IdGenerator;
import com.deer.wms.common.core.controller.BaseController;
import com.deer.wms.common.core.domain.AjaxResult;
import com.deer.wms.common.core.page.TableDataInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 条形码处理
 */
@Controller
@RequestMapping("/barCode")
public class BarCodeController extends BaseController {

    @Autowired
    private BarCodeService barCodeService;





    private String prefix = "bar";

    /**
     * 显示全部信息，条件查询
     * @param barCodeDto
     * @return
     */
    @PostMapping("/findList")
    @ResponseBody
    @Transactional
    public TableDataInfo findList(BarCodeDto barCodeDto) {
        startPage();
        List<BarCode> list = barCodeService.findBarCodeList(barCodeDto);
        return getDataTable(list);
    }

    /**
     * 新增跳转页面
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增
     * @param barCode
     * @return
     */
    @RequiresPermissions("out:barCode:add")
    @PostMapping("/insert")
    @ResponseBody
    @Transactional
    public AjaxResult insert( @RequestBody BarCode barCode){
        IdGenerator idGenerator = new DefaultIdGenerator();
        List<String> boxCodelist = Arrays.asList(barCode.getBoxCode().split(","));
        int rut = 0;
        //遍历传来的物料编码
        for (String boxCode:boxCodelist) {
            barCode.setBoxCode(boxCode);
            String path="C:\\Users\\L_dy\\Desktop\\"+boxCode+".jpg";
            String msg = idGenerator.next().substring(0,16);
            barCode.setBarCodeName(msg);
            barCode.setBorCodePath(path);
            BarCodeUtil.getBarCode(msg,path);
            rut = barCodeService.saveBarCode(barCode);
        }
        return toAjax(rut);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("out:barCode:remove")
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        return toAjax(barCodeService.delectBarCodeByIds(ids));
    }

    /**
     * 修改跳转页面
     */
    @GetMapping("/edit/{barCodeId}")
    public String edit(@PathVariable("barCodeId") Integer barCodeId, ModelMap mmap)
    {
        BarCode barCode = barCodeService.findBarCodeById(barCodeId);
        mmap.put("barCode", barCode);
        return prefix+"/edit";
    }

    /**
     * 修改保存条形码表信息
     */
    @RequiresPermissions("out:barCode:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BarCode barCode){
        return toAjax(barCodeService.saveBarCode(barCode));
    }


    /**
     *跳转显示页面
     * @return
     */
    @RequiresPermissions("out:barCode:view")
    @GetMapping("/page")
    public String BarCode()
    {
        return prefix + "/BarCode";
    }





}
